package com.haisen.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.haisen.core.utils.InsertBatch;
import com.haisen.core.utils.MyMapper;
import com.haisen.core.utils.PageResultSet;
import com.haisen.dataobject.entity.BussItem;
import com.haisen.dataobject.entity.CallOut;
import com.haisen.dataobject.entity.UserWeixin;
import com.haisen.dataobject.mapper.base.CallOutMapper;
import com.haisen.dataobject.mapper.base.UserWeixinMapper;
import com.haisen.dataobject.query.UserWeixinQuery;
import com.haisen.poi.basic_poi.BigExcelUtil;
import com.haisen.service.UserWeixinService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class UserWeixinServiceImpl implements UserWeixinService {


    @Autowired
    private UserWeixinMapper userWeixinMapper;
    @Autowired
    private SqlSessionFactory sqlSessionFactory;


    private InsertBatch<UserWeixin> batchAdd = null;

    @Override
    public void create(UserWeixin userWeixin) {
      userWeixinMapper.insertSelective(userWeixin);
    }

    @Override
    public void update(UserWeixin userWeixin) {
       userWeixinMapper.updateByPrimaryKeySelective(userWeixin);
    }

    @Override
    public void delete(Long[] ids) {
        Weekend<UserWeixin> example = Weekend.of(UserWeixin.class);
        WeekendCriteria<UserWeixin,Object> criteria = example.weekendCriteria();
        criteria.andIn(UserWeixin::getId, Arrays.asList(ids));
        userWeixinMapper.deleteByExample(example);
    }

    @Override
    public void createbatch(List<UserWeixin> userWeixinList) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
        MyMapper<UserWeixin> myMapper = (MyMapper<UserWeixin>) sqlSession.getMapper(UserWeixinMapper.class);
        batchAdd = new InsertBatch<>();
        batchAdd.insertBatch(sqlSession,myMapper,userWeixinList);

    }

    @Override
    public PageResultSet<UserWeixin> findByPage(UserWeixinQuery userWeixinQuery) {
           if(!StringUtils.isEmpty(userWeixinQuery.getOrderBy())){
               PageHelper.orderBy(userWeixinQuery.getOrderBy());
           }
         PageHelper.offsetPage(userWeixinQuery.getOffset(),userWeixinQuery.getLimit());
           Weekend<UserWeixin> example = Weekend.of(UserWeixin.class);
           WeekendCriteria<UserWeixin,Object> criteria = example.weekendCriteria();
           if (!StringUtils.isEmpty(userWeixinQuery.getPhone())){
             criteria.andEqualTo(UserWeixin::getPhone,userWeixinQuery.getPhone());
           }
          if (!StringUtils.isEmpty(userWeixinQuery.getShopNo())){
               criteria.andEqualTo(UserWeixin::getShopNo,userWeixinQuery.getShopNo());
          }
          //员工姓名
           if (!StringUtils.isEmpty(userWeixinQuery.getName())){
              criteria.andEqualTo(UserWeixin::getName,userWeixinQuery.getName());
           }
          if (!StringUtils.isEmpty(userWeixinQuery.getWeixinNo())){
              criteria.andLike(UserWeixin::getWeixinNo,"%"+userWeixinQuery.getWeixinNo()+"%");
          }

        //todo：日期大于，注意为字符串比较大小，是否要转换为日期型,小于和等于
        // log.info("{}",bussItemQuery.getSearchDate());
        if (!StringUtils.isEmpty(userWeixinQuery.getBeginDate())){
            criteria.andGreaterThanOrEqualTo(UserWeixin::getAddTime,userWeixinQuery.getBeginDate());
        }
        //小于截至日期
        if (!StringUtils.isEmpty(userWeixinQuery.getEndDate())){
            criteria.andLessThanOrEqualTo(UserWeixin::getAddTime,userWeixinQuery.getEndDate()+" 23:59:59");
        }
        PageResultSet<UserWeixin> resultSet = new PageResultSet<>();
        resultSet.setRows(userWeixinMapper.selectByExample(example));
        resultSet.setTotal(userWeixinMapper.selectCountByExample(example));
        return resultSet;
    }

    @Override
    public void importExcel2DataBase(String pathFile) {
        BigExcelUtil bigExcelUtil = BigExcelUtil.getInstance();
        List<Object> objs =  bigExcelUtil.importExcel2Obj(pathFile,UserWeixin.class);
        List<UserWeixin> weixinList = objs.stream()
                .filter(o -> {
                    if (!StringUtils.isEmpty(((UserWeixin)o).getName())) return true;
                    else return false;
                })
                .map( o ->{
                    log.info("读取excel行：{}",JSON.toJSONString(o));
                    return (UserWeixin)o;
                }).collect(Collectors.toList());
        createbatch(weixinList);
    }
}
